package leetcode;

import java.util.LinkedList;
import java.util.List;

public class Solution47 {

    private List<List<Integer>> res = new LinkedList<>();
    public List<List<Integer>> permuteUnique(int[] nums) {
        LinkedList<Integer> temp = new LinkedList<>();
        int[] used = new int[nums.length];
        backtrack(nums,temp,used);
        return res;
    }

    private void backtrack(int[] nums, LinkedList<Integer> temp, int[] used) {
        if(temp.size() == nums.length){
            if (!res.contains(new LinkedList<>(temp))) {
                res.add(new LinkedList<>(temp));
            }
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if(used[i] == 1){
                continue;
            }
            used[i] = 1;
            temp.add(nums[i]);
            backtrack(nums,temp,used);
            used[i] = 0;
            temp.removeLast();
        }
    }
}
